home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Online / SpeakFreely / src / libdes / cfb64ede.c < prev    next >
C/C++ Source or Header  |  2000-05-18  |  4KB  |  153 lines

  1. /* lib/des/cfb64enc.c */
  2. /* Copyright (C) 1995 Eric Young (eay@mincom.oz.au)
  3.  * All rights reserved.
  4.  * 
  5.  * This file is part of an SSL implementation written
  6.  * by Eric Young (eay@mincom.oz.au).
  7.  * The implementation was written so as to conform with Netscapes SSL
  8.  * specification.  This library and applications are
  9.  * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
  10.  * as long as the following conditions are aheared to.
  11.  * 
  12.  * Copyright remains Eric Young's, and as such any Copyright notices in
  13.  * the code are not to be removed.  If this code is used in a product,
  14.  * Eric Young should be given attribution as the author of the parts used.
  15.  * This can be in the form of a textual message at program startup or
  16.  * in documentation (online or textual) provided with the package.
  17.  * 
  18.  * Redistribution and use in source and binary forms, with or without
  19.  * modification, are permitted provided that the following conditions
  20.  * are met:
  21.  * 1. Redistributions of source code must retain the copyright
  22.  *    notice, this list of conditions and the following disclaimer.
  23.  * 2. Redistributions in binary form must reproduce the above copyright
  24.  *    notice, this list of conditions and the following disclaimer in the
  25.  *    documentation and/or other materials provided with the distribution.
  26.  * 3. All advertising materials mentioning features or use of this software
  27.  *    must display the following acknowledgement:
  28.  *    This product includes software developed by Eric Young (eay@mincom.oz.au)
  29.  * 
  30.  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  31.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  32.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  34.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  35.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  36.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  37.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  38.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  39.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  40.  * SUCH DAMAGE.
  41.  * 
  42.  * The licence and distribution terms for any publically available version or
  43.  * derivative of this code cannot be changed.  i.e. this code cannot simply be
  44.  * copied and put under another distribution licence
  45.  * [including the GNU Public Licence.]
  46.  */
  47.  
  48. #include "des_locl.h"
  49.  
  50. /* The input and output encrypted as though 64bit cfb mode is being
  51.  * used.  The extra state information to record how much of the
  52.  * 64bit block we have used is contained in *num;
  53.  */
  54.  
  55. void des_ede3_cfb64_encrypt(in, out, length, ks1,ks2,ks3, ivec, num, encrypt)
  56. unsigned char *in;
  57. unsigned char *out;
  58. long length;
  59. des_key_schedule ks1,ks2,ks3;
  60. des_cblock (*ivec);
  61. int *num;
  62. int encrypt;
  63.     {
  64.     register unsigned long v0,v1;
  65.     register long l=length,n=*num;
  66.     unsigned long ti[2];
  67.     unsigned char *iv,c,cc;
  68.  
  69.     iv=(unsigned char *)ivec;
  70.     if (encrypt)
  71.         {
  72.         while (l--)
  73.             {
  74.             if (n == 0)
  75.                 {
  76.                 c2l(iv,v0);
  77.                 c2l(iv,v1);
  78.  
  79.                 IP(v0,v1);
  80.  
  81.                 ti[0]=v0;
  82.                 ti[1]=v1;
  83.                 des_encrypt2((unsigned long *)ti,ks1,DES_ENCRYPT);
  84.                 des_encrypt2((unsigned long *)ti,ks2,DES_DECRYPT);
  85.                 des_encrypt2((unsigned long *)ti,ks3,DES_ENCRYPT);
  86.                 v0=ti[0];
  87.                 v1=ti[1];
  88.  
  89.                 FP(v1,v0);
  90.  
  91.                 iv=(unsigned char *)ivec;
  92.                 l2c(v0,iv);
  93.                 l2c(v1,iv);
  94.                 iv=(unsigned char *)ivec;
  95.                 }
  96.             c= *(in++)^iv[n];
  97.             *(out++)=c;
  98.             iv[n]=c;
  99.             n=(n+1)&0x07;
  100.             }
  101.         }
  102.     else
  103.         {
  104.         while (l--)
  105.             {
  106.             if (n == 0)
  107.                 {
  108.                 c2l(iv,v0);
  109.                 c2l(iv,v1);
  110.  
  111.                 IP(v0,v1);
  112.  
  113.                 ti[0]=v0;
  114.                 ti[1]=v1;
  115.                 des_encrypt2((unsigned long *)ti,ks1,DES_ENCRYPT);
  116.                 des_encrypt2((unsigned long *)ti,ks2,DES_DECRYPT);
  117.                 des_encrypt2((unsigned long *)ti,ks3,DES_ENCRYPT);
  118.  
  119.                 v0=ti[0];
  120.                 v1=ti[1];
  121.  
  122.                 FP(v1,v0);
  123.  
  124.                 iv=(unsigned char *)ivec;
  125.                 l2c(v0,iv);
  126.                 l2c(v1,iv);
  127.                 iv=(unsigned char *)ivec;
  128.                 }
  129.             cc= *(in++);
  130.             c=iv[n];
  131.             iv[n]=cc;
  132.             *(out++)=c^cc;
  133.             n=(n+1)&0x07;
  134.             }
  135.         }
  136.     v0=v1=ti[0]=ti[1]=c=cc=0;
  137.     *num=n;
  138.     }
  139.  
  140. #ifdef undef /* MACRO */
  141. void des_ede2_cfb64_encrypt(in, out, length, ks1,ks2, ivec, num, encrypt)
  142. unsigned char *in;
  143. unsigned char *out;
  144. long length;
  145. des_key_schedule ks1,ks2;
  146. des_cblock (*ivec);
  147. int *num;
  148. int encrypt;
  149.     {
  150.     des_ede3_cfb64_encrypt(in,out,length,ks1,ks2,ks1,ivec,num,encrypt);
  151.     }
  152. #endif
  153.